/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service.tmessage;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.wechat.entity.tmessage.BusinessMessageTemplate;
import com.boxin.ims.modules.wechat.dao.tmessage.BusinessMessageTemplateDao;

/**
 * 系统消息模板Service
 * @author 肖乾
 * @version 2014-09-11
 */
@Component
@Transactional(readOnly = true)
public class BusinessMessageTemplateService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(BusinessMessageTemplateService.class);
	
	@Autowired
	private BusinessMessageTemplateDao businessMessageTemplateDao;
	
	public BusinessMessageTemplate get(Long id) {
		return businessMessageTemplateDao.findOne(id);
	}
	
	public Page<BusinessMessageTemplate> find(Page<BusinessMessageTemplate> page, BusinessMessageTemplate businessMessageTemplate) {
		DetachedCriteria dc = businessMessageTemplateDao.createDetachedCriteria();
		if (businessMessageTemplate.getUser()!=null && businessMessageTemplate.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", businessMessageTemplate.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(businessMessageTemplate.getName())){
			dc.add(Restrictions.like("name", "%"+businessMessageTemplate.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return businessMessageTemplateDao.find(page, dc);
	}
	
	
	public List<BusinessMessageTemplate> find( BusinessMessageTemplate businessMessageTemplate) {
		DetachedCriteria dc = businessMessageTemplateDao.createDetachedCriteria();
		if (businessMessageTemplate.getUser()!=null && businessMessageTemplate.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", businessMessageTemplate.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(businessMessageTemplate.getName())){
			dc.add(Restrictions.like("name", "%"+businessMessageTemplate.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return businessMessageTemplateDao.find(dc);
	}
	
	@Transactional(readOnly = false)
	public void save(BusinessMessageTemplate businessMessageTemplate) {
		businessMessageTemplateDao.save(businessMessageTemplate);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		businessMessageTemplateDao.deleteById(id);
	}
	
}
